<?php
// validate.class.php - form validation class

class validate_form {
	
	var $requiredFields = array();
	var $requiredMatchFields = array();
	
	function setRequired($inputArray) {
			$this->requiredFields = $inputArray["fields"];
			$this->requiredMatchFields = $inputArray["match"];
			/* Debug Print *
			echo '<pre>';
			print_r($this->requiredFields);
			echo '<br>';
			print_r($this->requiredMatchFields);
			echo '</pre>';
			// End Debug Print */
	}
	
	function validate($inputArray) {
		$invalid_items = array();
		//print_r($inputArray);
		$validated = TRUE;
		foreach ($inputArray as $fieldKey => $fieldValue) {
			foreach ($this->requiredFields as $requiredField) {
				//echo 'required: ' . $requiredField . '<br>';
				if ($fieldKey == $requiredField) {
					//echo 'found - value:' . $fieldValue . '<br />';
					if ($fieldValue == "") {
						$validated = FALSE;
						//echo 'invalid<br>';
						$invalidPair = array($fieldKey => $fieldValue);
						array_push($invalid_items, $invalidPair);
						//print_r($invalid_items);
					}
				}
			}
		}
		//echo 'invalid items: ';
		//print_r($invalid_items);
		if ($validated == FALSE) {
			return $invalid_items;	
		} else {
			return "valid";
		}
	}
	
	function validateMatched($inputArray) {
		$validated = TRUE;
		$check_1 = $this->requiredMatchFields[1];
		$check_2 = $this->requiredMatchFields[2];
		if ($inputArray[$check_1] != $inputArray[$check_2]) {
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
	function combinePostFields($inputArray) {
		$return_array = array();
		//print_r($inputArray);
		foreach ($inputArray as $key => $value) {
			trim($value);
			$temp_value = $key . "=" . urlencode($value);
			array_push($return_array,$temp_value);	
		}
		$return_string = implode('&',$return_array);
		return $return_string;
	}
	
	function repostFormInvalid($postTo,$reason,$postData) {
		$rePostData = $this->combinePostFields($postData);
		$rePostData .= '&validated=NO&reason=' . $reason;
		//echo 'Reposting to ' . $postTo . ' for ' . $reason . ' with post data ' . $rePostData;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $postTo);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $rePostData);
		curl_exec($ch);
		
		
		curl_close($ch);
	}
	
}
?>